fit_pandas_GUI()¶You can try this notebook live by lauching it in Binder.This can take a while to launch, be patient.
.
First we import pandas, numpy and pandas_GUI and then create some data to fit.
import pandas as pd
from pandas_GUI import *
import numpy as np
X = [k*np.pi/10 for k in range(-30,30)]
Y = 98*np.sin(X)+np.random.default_rng().normal(0,3,60)
df = pd.DataFrame({'X':X, 'Y':Y})
This is the plot made using the default settings of plot_pandas_GUI(). See step-by-step example.
# CODE BLOCK generated using plot_pandas_GUI(). See https://github.com/JupyterPhysSciLab/jupyter_Pandas_GUI.
from plotly import graph_objects as go
Figure_1 = go.FigureWidget(layout_template="simple_white")
scat = go.Scatter(x = df['X'], y = df['Y'],
mode = 'lines', name = 'Noisy Sine',)
Figure_1.add_trace(scat)
Figure_1.update_xaxes(title= 'Time')
Figure_1.update_yaxes(title= 'Amplitude')
Figure_1.update_layout(title = 'Noisy Sine Wave Default Plot', template = 'simple_white')
Figure_1.show()
# Force save widget states so that graph will still be
# available when notebook next opened in trusted state.
JPSLUtils.OTJS('Jupyter.actions.call("widgets:save-with-widgets");')
Figure 1: A plot of the noisy sine wave data. This should display a live plotly plot. If you are running a live notebook and do not see the plot make sure you have trusted the notebook (button near the kernel name and status).
labels for the X and Y axis were input and the Display Mirror Axes box was checked.

the final checks were done and then the 'Do Fit' button was clicked, closing the GUI and running the code in the cell below to perform the fit and display the results.

# CODE BLOCK generated using fit_pandas_GUI(). See https://github.com/JupyterPhysSciLab/jupyter_Pandas_GUI.
# Imports (no effect if already imported)
import numpy as np
import lmfit as lmfit
import round_using_error as rue
import copy as copy
from plotly import graph_objects as go
from IPython.display import HTML
# Define data and trace name
Xvals = df["X"]
Yvals = df["Y"]
tracename = "Noisy Sine"
# Define error (uncertainty)
Yerr = df["Y"]*0 + 1
# Define the fit model, initial guesses, and contraints
fitmod = lmfit.models.SineModel()
fitmod.set_param_hint("amplitude", vary = True, min = 0.0)
fitmod.set_param_hint("frequency", vary = True, min = 0.0)
fitmod.set_param_hint("shift", vary = True, min = -6.283195307179586, max = 6.283195307179586)
# Do fit
Fit_1 = fitmod.fit(Yvals, x=Xvals, weights = 1/Yerr, scale_covar = True, nan_policy = "omit")
# Calculate residuals (data - fit) because lmfit
# does not calculate for all points under all conditions
resid = []
for i in range(0,len(Fit_1.data)):
resid.append(Fit_1.data[i]-Fit_1.best_fit[i])
# Plot Results
Fit_1_Figure = go.FigureWidget(layout_template="simple_white")
Fit_1_Figure.update_layout(title = "Fit_1_Figure")
Fit_1_Figure.set_subplots(rows=2, cols=1, row_heights=[0.2,0.8], shared_xaxes=True)
scat = go.Scatter(y=resid,x=Xvals, mode="markers",name = "residuals")
Fit_1_Figure.update_yaxes(title = "Residuals", row=1, col=1, zeroline=True, zerolinecolor = "lightgrey", mirror = True)
Fit_1_Figure.update_xaxes(row=1, col=1, mirror = True)
Fit_1_Figure.add_trace(scat,col=1,row=1)
scat = go.Scatter(x=Xvals, y=Yvals, mode="markers", name=tracename)
Fit_1_Figure.add_trace(scat, col=1, row = 2)
Fit_1_Figure.update_yaxes(title = "Y", row=2, col=1, mirror = True)
Fit_1_Figure.update_xaxes(title = "X", row=2, col=1, mirror = True)
scat = go.Scatter(y=Fit_1.best_fit,x=Xvals, mode="lines", name="fit", line_color = "black", line_dash="solid")
Fit_1_Figure.add_trace(scat,col=1,row=2)
Fit_1_Figure.show()
# Display best fit equation
ampstr = ''
freqstr = ''
shiftstr = ''
for k in Fit_1.params.keys():
if Fit_1.params[k].vary:
paramstr = '(\color{red}{'+rue.latex_rndwitherr(Fit_1.params[k].value,
Fit_1.params[k].stderr,
errdig=1,lowmag=-3)+'})'
else:
paramstr = '\color{blue}{'+str(Fit_1.params[k].value)+'}'
if k == 'amplitude':
ampstr = paramstr
if k == 'frequency':
freqstr = paramstr
if k == 'shift' and Fit_1.params[k].value != 0:
shiftstr = ' + ' + paramstr
fitstr = r'$fit = '+ampstr + 'sin[' + freqstr + 'x' + shiftstr + ']$'
captionstr = '<p>Use the command <code>Fit_1</code> as the last line of a code cell for more details.</p>'
display(HTML(fitstr+captionstr))
Use the command Fit_1 as the last line of a code cell for more details.
Figure 2: The results of fitting the noisy sine data to using default settings. Alternative formatting of line styles, markers, etc... can be accessed by editing the code produced by the GUI. Overall plot styling can be adjusted on tab 5.
In addition to trying it below if this is a live notebook, you can look at the other examples listed in the Pandas GUI website.
If you are running this notebook live in binder you can try it here by running the first cell to import the tools and create the data. Then run the cell below to create the GUI. Note: You may want to expand the collapsed instructions to learn more about each tab.
fit_pandas_GUI()